﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using schedulelist.ORM.dao.classes;
using schedulelist.ORM.dao.interfaces;
using schedulelist.ORM.mapping;
using schedulelist.ORM.dao;
using schedulelist.ORM.domain;

namespace schedulelist
{
    public class Interlayer : System.Web.UI.Page
    {
        public DAOFactory getFactory()
        {
            ISession session = (ISession)Session["hbmsession"];
            DAOFactory factory = new NHibernateDAOFactory(session);
            return factory;
        }

        protected IUserDAO UDAO() {
            return getFactory().getUserDAO();
        }

        protected ITicketsDAO TDAO()
        {
            return getFactory().getTicketsDAO();
        }

        public bool isLogged() {
            return UDAO().getUserByLogin(Login.CurrentLog) == null;
        }

        public User loggedUser()
        {
            User res = UDAO().getUserByName(Profile.uname);
            return res;
        }

        public string getUserName() {
            return loggedUser().name;
        }

        public string getUserLogin(){
            return loggedUser().login;
        }

        public string getUserEmail(){
            return loggedUser().email;
        }

        public string getUserSex(){
            return loggedUser().sex.ToString();
        }

        public string getUserRole(){
            return loggedUser().Role.name;
        }

        public string getUserDep()
        {
            User res = UDAO().getUserByName(Profile.uname);
            return res.Dep.name;
        }

        public string GetTicsOfLoggedUser() {
            return UDAO().GetTicsCountOfUser(loggedUser()).ToString();
        }


        public string GetCompletedTicsOfLoggedUser()
        {
            return UDAO().GetCompletedTicsCountOfUser(loggedUser()).ToString();
        }


        public void Fill_DropDownList(DropDownList d)
        {
            foreach (Customer c1 in getFactory().getCustomerDAO().GetAll())
            {
                //Добавляем в DropDownList роли все записи из таблиц "Customer"
                d.Items.Add(c1.name);
            }
        
        }
        public IList<User> GetUserListByDep()
        {
            //Получаем юзера по логину
            IList<User> ulist = UDAO().getUsersByDep(loggedUser().Dep);
            ulist.Remove(loggedUser());
            return ulist;
        }
        public string getTicketsByOwner()
        {
            int cnt=0;
            foreach (User u in GetUserListByDep())
            {
                cnt = cnt + TDAO().getTicketsByOwner(u).Count;
            }
            return cnt.ToString();
        }

        public string GetTicsCountOfUser()
        {
            int cnt = 0;
            foreach (User u in GetUserListByDep())
            {
                cnt = cnt + UDAO().GetTicsCountOfUser(u);
            }
            return cnt.ToString();
        }

        public string GetCompletedTicsCountOfUser()
        {
            int cnt = 0;
            foreach (User u in GetUserListByDep())
            {
                cnt = cnt + UDAO().GetCompletedTicsCountOfUser(u);
            }
            return cnt.ToString();
        }


        public int GetTicsOfownByDate(string str) {
            int cnt = UDAO().GetTicsOfownByDate(loggedUser(), str);
            return cnt;
        }

        public int GetTicsOfExByDate(string str)
        {
            int cnt = UDAO().GetTicsOfExByDate(loggedUser(), str);
            return cnt;
        }


        public string getTicketsCountByDepForCustomer(string CustNameIn_DDL1) {
            IList<User> ulist = UDAO().getUsersByDep(loggedUser().Dep);
            int cnt = 0;
            foreach (User u in ulist)
            {
                foreach (Tickets t in TDAO().getTicketsByOwner(u))
                {
                    if (t.cust.name == CustNameIn_DDL1)
                    {
                        cnt = cnt + 1;
                    }
                }
            }
            return cnt.ToString();
        }

    }
}